<?php
include "general_include.php";
include_once("adodb/dbconfig.php");

if(!isset($_SESSION['user_id'])){
	echo "session not started";
	return;
}
function getPostOrGetParam($param){
	if(isset($_POST[$param]))
		return $_POST[$param];
	else{
		if(isset($_GET[$param]))
			return $_GET[$param];
		else
			return "";
	}
}
/*
function addAsFriend($userId){
	$ret = array();
	try{
		$addAsFriendSql="INSERT INTO ".TABLEPREFIX."_user_friends_list (nk_user_id_owner,nk_user_id_friend,friend_status) VALUES('".$_SESSION['user_id']."','".$userId."',1)";
		logToFile("addAsFriendSql: ".$addAsFriendSql);
		//"SELECT * FROM ".TABLEPREFIX."_user_friends_list WHERE (nk_user_id_owner='".$_SESSION['user_id']."' or nk_user_id_friend='".$_SESSION['user_id']."') and (nk_user_id_owner='".$userId."' or nk_user_id_friend='".$userId."')"
		
		
		$friendsSql = "SELECT * FROM ".TABLEPREFIX."_user_friends_list WHERE nk_user_id_owner='".$_SESSION['user_id']."' or nk_user_id_friend='".$_SESSION['user_id']."'";
		
		$friendsArr = $UserManagerObjAjax->GetRecords("All",$friendsSql);
		$isfriend = "n";
		for($t=0;$t<count($friendsArr);$t++){
			if($friendsArr[$t]['nk_user_id_friend'] == $userId || $friendsArr[$t]['nk_user_id_owner'] == $userId){
				$ret['IsSuccess'] = false;
				$ret['Msg'] = "Already in friend list";
				return $ret;
			}
		}

		
		$addAsFriend=$UserManagerObjAjax->Execute($addAsFriendSql);
		
		$ret['Msg'] = mysql_insert_id();
		
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	logToFile("mysql_insert_id: ".$ret['Msg']);
	return $ret;
}
*/
function addAsFriend($user){
	$ret = array();
	try{
		$db = new DBConnection();
		$db->getConnection();
		$sql = "INSERT INTO `nk_user_friends_list` (`nk_user_id_owner`,`nk_user_id_friend`,`friend_status`, `cdate`) VALUES('".$_SESSION['user_id']."','".$user."', 2, SYSDATE());";
		
// 		logToFile("SQL: ".$sql);
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'add success';
			$ret['Id'] = mysql_insert_id();
		}

	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}

	return $ret;
}

function removeFromFriendList($user){
	$ret = array();
	try{
		$db = new DBConnection();
		$db->getConnection();
// 		$sql = "INSERT INTO `nk_user_friends_list` (`nk_user_id_owner`,`nk_user_id_friend`,`friend_status`, `cdate`) VALUES('".$_SESSION['user_id']."','".$user."', 2, SYSDATE());";
	
		$sql = "DELETE FROM `nk_user_friends_list` WHERE (nk_user_id_owner='".$_SESSION['user_id']."' or nk_user_id_friend='".$_SESSION['user_id']."') and (nk_user_id_owner='".$user."' or nk_user_id_friend='".$user."')";
		
// 		logToFile("SQL: ".$sql);
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'add success';
// 			$ret['Id'] = mysql_insert_id();
		}

	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}

	return $ret;
}


function changeOnlineStatus($status){
	$ret = array();
	try{
		$sql = "UPDATE ".TABLEPREFIX."_user SET online_status = ".$status." WHERE user_id = ".$_SESSION['user_id'];
// 		logToFile("sql: ".$sql);
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'updated';
			$ret['Data'] = $status;
		}
		
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}

	return $ret;
}

function addPhonoSessionId($sessionId){
	$ret = array();
	try{
		$sql = "UPDATE ".TABLEPREFIX."_user SET phono_session = '".$sessionId."' WHERE user_id = ".$_SESSION['user_id'];
// 		logToFile("sql: ".$sql);
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] ="Error: ".mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = $_SESSION['user_id'].'==>'.$sessionId;
		}
	
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	//print_r($ret);
	return $ret;
}

function getUser($userId){
	$ret = array();
	try{
		$sql = "SELECT * FROM ".TABLEPREFIX."_user WHERE user_id = ".$userId;
// 		logToFile("sql: ".$sql);
		$handle = mysql_query($sql);
		return mysql_fetch_object($handle);
	
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	//print_r($ret);
	return $ret;
}


if(isset($_GET['method']) || isset($_POST['method'])){
	header('Content-type:text/javascript;charset=UTF-8');

	$method = getPostOrGetParam("method");

	switch ($method) {
		case "addAsFriend":
			$ret = addAsFriend(getPostOrGetParam('userid'));	
			break;
		case "changeOnlineStatus":
			$ret = changeOnlineStatus(getPostOrGetParam('status'));	
			break;
		case "addPhonoSessionId":
			$ret = addPhonoSessionId(getPostOrGetParam('sessionId'));
			break;
		case "getUser":
			$ret = getUser(getPostOrGetParam('userId'));
			break;
		case "removeFriend":
			$ret = removeFromFriendList(getPostOrGetParam('userid'));
			break;
	
	}
	echo json_encode($ret);
}
?>
